package com.powernode.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * @Description:
 * @Author: Tong Ziyu
 * @Date: 2024/10/26 19:40
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SecurityUser implements UserDetails {

    // 系统管理员用户信息
    private Long userId;

    private String username;

    private String password;

    private Integer status;

    private Long shopId;

    private String loginType;

    private Set<String> perms;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.emptyList();
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.loginType + this.userId;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }

    /**
     *
     * @return
     */
    public Set<String> getPerms() {

        Set<String> splitPerms = new HashSet<>();
        // 将set里面的字符串,根据, 分割出来
        for (String perm : this.perms){
            // 判断是否包含, 如果包含则进行分割
            if(perm.contains(",")){
                String[] split = perm.split(",");
                for (String s : split){
                    splitPerms.add(s);
                }
            }else{
                splitPerms.add(perm);
            }
        }
        return splitPerms;
    }
}
